﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.CMS.Process.DataEntity;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;
using TjZheng.Power.RedisEntity;
using TjZheng.Redis;
using TjZheng.Framework;

namespace TjZheng.CMS.Process.Business
{
    public class UserRedisProcess
    {
        /// <summary>
        /// 用户相关权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTrees(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            List<PowerInfo> objPowerInfoList = CreatePowerInfoList(targetId, objUserPowerType);
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            return objPowerInfoList.Where(c => isContainButton ? true : c.PowerType != 2).OrderBy(c => c.SortIndex).ToList().ConvertToTreeList(id) ?? new List<TreeEntity>();
        }

        /// <summary>
        /// 除隐藏权限的用户相关权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreesWithoutUnDisplay(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            List<PowerInfo> objPowerInfoList = CreatePowerInfoList(targetId, objUserPowerType);
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            return objPowerInfoList.Where(c => (isContainButton ? true : c.PowerType != 2) && c.IsDisplay == 1).OrderBy(c => c.SortIndex).ToList().ConvertToTreeList(id) ?? new List<TreeEntity>();
        }

        /// <summary>
        /// 除根节点的用户权限列表树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreeList(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            List<PowerInfo> objPowerInfoList = CreatePowerInfoList(targetId, objUserPowerType);
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            return objPowerInfoList.Where(c => isContainButton ? true : c.PowerType != 2).OrderBy(c => c.SortIndex).ToList().ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }

        /// <summary>
        /// 除根节点和隐藏权限的用户权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainButton"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreeListWithoutUnDisplay(long id, long targetId, bool isContainButton, UserPowerType objUserPowerType)
        {
            List<PowerInfo> objPowerInfoList = CreatePowerInfoList(targetId, objUserPowerType);
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            return objPowerInfoList.Where(c => (isContainButton ? true : c.PowerType != 2) && c.IsDisplay == 1).OrderBy(c => c.SortIndex).ToList().ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }

        /// <summary>
        /// 除父节点和按钮的用户子权限列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="targetId"></param>
        /// <param name="isContainUnDisplay"></param>
        /// <param name="objUserPowerType"></param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoChildrenWithoutButton(long id, long targetId, bool isContainUnDisplay, UserPowerType objUserPowerType)
        {
            List<PowerInfo> objPowerInfoList = CreatePowerInfoList(targetId, objUserPowerType);
            if (objPowerInfoList.IsNull())
            {
                return new List<TreeEntity>();
            }
            return objPowerInfoList.Where(c => c.PowerType != 2 && c.ParentId == id && (isContainUnDisplay ? true : c.IsDisplay == 1)).OrderBy(c => c.SortIndex).ToList().ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }

        public List<PowerInfo> CreatePowerInfoList(long targetId, UserPowerType objUserPowerType)
        {
            UserPower objUserPower = null;
            List<PowerInfo> objPowerInfoList = null;
            switch (objUserPowerType)
            {
                case UserPowerType.GroupPower:
                    GroupPower objGroupPower = RedisHelper.GetById<GroupPower>(targetId, PowerVariable.ProviderName, PowerVariable.Db);
                    if (objGroupPower != null && objGroupPower.PowerIds.IsNoNull())
                    {
                        objPowerInfoList = objGroupPower.PowerIds.CreatePowerInfoList();
                    }
                    break;
                case UserPowerType.PartGroupUserPower:
                    objUserPower = RedisHelper.GetById<UserPower>(targetId, PowerVariable.ProviderName, PowerVariable.Db);
                    if (objUserPower != null && objUserPower.PowerIds.IsNoNull())
                    {
                        objPowerInfoList = objUserPower.PowerIds.CreatePowerInfoList();
                    }
                    break;
                case UserPowerType.DependentUserPower:
                    objUserPower = RedisHelper.GetById<UserPower>(targetId, PowerVariable.ProviderName, PowerVariable.Db, "Dependent");
                    if (objUserPower != null && objUserPower.PowerIds.IsNoNull())
                    {
                        objPowerInfoList = objUserPower.PowerIds.CreatePowerInfoList();
                    }
                    break;
                case UserPowerType.GroupUserPower:
                    BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
                    //var objGroupIdList = objBizUserRoleInfo.GetFieldListCache<long, UserRoleInfo, long>("", "UserId=" + targetId, "GroupId");                    
                    var objUserRole = RedisHelper.GetById<UserRole>(targetId, PowerVariable.ProviderName, PowerVariable.Db);
                    if (objUserRole != null && objUserRole.GroupIds.IsNoNull())
                    {
                        var objGroupPowerList = RedisHelper.GetByIds<GroupPower>(objUserRole.GroupIds, PowerVariable.ProviderName, PowerVariable.Db);
                        if (objGroupPowerList.IsNoNull())
                        {
                            objPowerInfoList = objGroupPowerList.SelectMany(c => c.PowerIds).Distinct().ToList().CreatePowerInfoList();
                        }
                    }
                    break;
            }
            return objPowerInfoList;
        }
    }
}
